package com.westcom.vulb.activity;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

import com.westcom.vulb.bean.Card;
import com.westcom.vulb.bean.Card.ImageCardResource;
import com.westcom.vulb.logic.CardDetail;
import com.westcom.vulb.logic.SessionManager;
import com.westcom.vulb.logic.card;
import com.westcom.vulb.logic.comment;
import com.westcom.vulb.logic.resource;
import com.westcom.vulb.protocal.http.Response;

@SuppressLint("ResourceAsColor")
public class CardInteractActivity extends BasicActivity {
    public static final int TAB_VIEW        = 0;
    public static final int TAB_GOOD        = 1;
    public static final int TAB_COMMENT     = 2;
    
    public static final String PARAM_TAB_TYPE   = "tab_type";
    public static final String PARAM_CARD_ID    = "card_id";
    
    private int mTabIndex = -1;
    public int mCardId = 0;
    
    private HashMap<Integer, CardInfoBasicFragment> mFragmentMap;
    private CardInfoBasicFragment mCurrFragment;
    private LinearLayout [] mTopBarArray;
    
    private TextView mViewTabView;
    private TextView mGoodTabView;
    private TextView mCommentTabView;
    
    private TextView tv_view_edit,tv_view_edit_done,tv_comment_edit;
    private ImageView iv_back,iv_view_dele,iv_good_send_good;
    private LinearLayout ll_view_edit;
    private boolean mIsViewEditMode = false;
    private boolean mIsCommentEditMode = false;
    public static final int MSG_UPDATE_CARD = 0x01;
    public static final int REQUEST_IMAGE_DETAIL = 0x11;
    
    private SessionManager sm = null;
    private OnClickListener l = new OnClickListener()
    {

		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			switch(v.getId())
			{
				case R.id.card_back:
				{
					finish();
				}break;
				case R.id.tv_card_interact_view_edit:
				{
					mIsViewEditMode = !mIsViewEditMode;
					CardDetailFragment fragment = (CardDetailFragment) mFragmentMap.get(TAB_VIEW);
					fragment.SwitchMod();
				}break;
				case R.id.iv_card_interact_view_dele:
				{
					
				}break;
				case R.id.tv_card_interact_view_edit_done:
				{
					mIsViewEditMode = !mIsViewEditMode;
					CardDetailFragment fragment = (CardDetailFragment) mFragmentMap.get(TAB_VIEW);
					fragment.SwitchMod();
					fragment.updateData();
				}break;
				case R.id.iv_card_interact_good_send_good:
				{
					CardGoodFragment fgt = (CardGoodFragment)mFragmentMap.get(TAB_GOOD);
					fgt.valuatCard(mCardId);
				}break;
				case R.id.tv_card_interact_comment_edit:
				{
					mIsCommentEditMode = !mIsCommentEditMode;
					CardCommentFragment fgt = (CardCommentFragment)mFragmentMap.get(TAB_COMMENT);
					fgt.SwitchMod();
				}break;
				default:{}break;
			}
			if(mIsViewEditMode)
			{
				ll_view_edit.setVisibility(View.GONE);
				tv_view_edit_done.setVisibility(View.VISIBLE);
			}else
			{
				ll_view_edit.setVisibility(View.VISIBLE);
				tv_view_edit_done.setVisibility(View.GONE);
			}
			if(mIsCommentEditMode)
			{
				tv_comment_edit.setText(R.string.done);
			}else
			{
				tv_comment_edit.setText(R.string.edit);
			}
		}
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        int tabIndex = getIntent().getIntExtra(PARAM_TAB_TYPE, TAB_VIEW);
        mCardId = getIntent().getIntExtra(PARAM_CARD_ID, 8172);
        
        setContentView(R.layout.card_interact_activity);
        mViewTabView = (TextView) findViewById(R.id.interact_tab_view);
        mGoodTabView = (TextView) findViewById(R.id.interact_tab_good);
        mCommentTabView = (TextView) findViewById(R.id.interact_tab_comment);
        
        mLoadingDialog.setMessage(getResources().getString(R.string.loading));
        
        findViewById(R.id.card_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });  
        mFragmentMap = new HashMap<Integer, CardInfoBasicFragment>();
        mFragmentMap.put(TAB_VIEW, new CardDetailFragment());
        mFragmentMap.put(TAB_GOOD, new CardGoodFragment());
        mFragmentMap.put(TAB_COMMENT, new CardCommentFragment());
        
        mTopBarArray = new LinearLayout[mFragmentMap.size()];
        mTopBarArray[0] = (LinearLayout)this.findViewById(R.id.ll_card_interact_view_bar);
        mTopBarArray[1] = (LinearLayout)this.findViewById(R.id.ll_card_interact_good_bar);
        mTopBarArray[2] = (LinearLayout)this.findViewById(R.id.ll_card_interact_comment_bar);
        
        setTabIndex(tabIndex);
        
        iv_back = (ImageView)findViewById(R.id.card_back);
        iv_back.setOnClickListener(l);
        //view bar
        ll_view_edit = (LinearLayout)findViewById(R.id.ll_card_interact_edit);
        tv_view_edit = (TextView)findViewById(R.id.tv_card_interact_view_edit);
        tv_view_edit_done = (TextView)findViewById(R.id.tv_card_interact_view_edit_done);
        iv_view_dele = (ImageView)findViewById(R.id.iv_card_interact_view_dele);
        tv_view_edit.setOnClickListener(l);
        tv_view_edit_done.setOnClickListener(l);
        iv_view_dele.setOnClickListener(l);
        //good bar
        iv_good_send_good = (ImageView)findViewById(R.id.iv_card_interact_good_send_good);
        iv_good_send_good.setOnClickListener(l);
        //comment bar
        tv_comment_edit = (TextView)findViewById(R.id.tv_card_interact_comment_edit);
        tv_comment_edit.setOnClickListener(l);
        
        sm = SessionManager.getInstance();
    }
    
    private void setTabIndex(int tabIndex) 
    {
        if (mTabIndex == tabIndex) 
        {
            return;
        }
        
        mCurrFragment = mFragmentMap.get(tabIndex);
        if (mCurrFragment == null) 
        {
            return;
        }
        mTabIndex = tabIndex;
        for(int i = 0;i<mTopBarArray.length;i++)
        {
        	if(i == tabIndex)
        	{
        		mTopBarArray[i].setVisibility(View.VISIBLE);
        	}else
        	{
        		mTopBarArray[i].setVisibility(View.GONE);
        	}
        }
        renderTabBar();
        
        FragmentManager fgManager = getFragmentManager();
        FragmentTransaction ts = fgManager.beginTransaction();
        ts.replace(R.id.interact_content, mCurrFragment);
        ts.commit();
    }
    
    public void onTabViewSelected(View v) {
        setTabIndex(TAB_VIEW);
    }
    
    public void onTabGoodSelected(View v) {
        setTabIndex(TAB_GOOD);
    }
    
    public void onTabCommentSelected(View v) {
        setTabIndex(TAB_COMMENT);
    }
    
    private void renderTabBar() 
    {
    	CardDetailFragment fragment = (CardDetailFragment) mFragmentMap.get(TAB_VIEW);
    	Card card = fragment.getCardDetail();
        String desc = getString(R.string.card_interact_tab_good);
        int goodCount = 0;
        if (card != null) 
        {
            goodCount = card.goodCount;
        }
        if (goodCount > 0) {
            desc = String.format("%s(%d)", getString(R.string.card_interact_tab_good), goodCount);
        }
        mGoodTabView.setText(desc);
    
        desc = getString(R.string.card_interact_tab_comment);
        int commentCount = 0;
        if (card != null) 
        {
            commentCount = card.commentCount;
        }
        if (commentCount > 0) 
        {
            desc = String.format("%s(%d)", getString(R.string.card_interact_tab_comment), commentCount);
        }
        mCommentTabView.setText(desc);
        
        ColorStateList normalColor = getResources().getColorStateList(R.color.card_tabbar_font_color);
        ColorStateList lightColor = getResources().getColorStateList(R.color.card_tabbar_font_color_light);
        mViewTabView.setTextColor(normalColor);
        mGoodTabView.setTextColor(normalColor);
        mCommentTabView.setTextColor(normalColor);
        switch (mTabIndex)
        {
            case TAB_VIEW:
                mViewTabView.setTextColor(lightColor);
                break;
            case TAB_GOOD:
                mGoodTabView.setTextColor(lightColor);
                break;
            case TAB_COMMENT:
                mCommentTabView.setTextColor(lightColor);
                break;
            default:
                break;
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
		switch (resultCode) 
		{
			case Activity.RESULT_OK:
			{
				switch(requestCode)
				{
					case REQUEST_IMAGE_DETAIL:
					{
						int resId = data.getIntExtra("resId", 0);
						boolean isDele = data.getBooleanExtra("delete", false);
						if(isDele && resId>0)
						{
							CardDetailFragment fragment = (CardDetailFragment) mFragmentMap.get(TAB_VIEW);
							int index = fragment.getCardDetail().getImageResIndex(resId);
							if(index >0)
							{
								fragment.getCardDetail().resources.remove(index);
								fragment.updateData();
							}
						}	
					}break;
					default:{}break;
				}				
			}break;
		}
    }
    public void onDetailLoaded()
    {
    	renderTabBar();
    }
    public void setGoodButtonImg(int resId)
    {
    	this.iv_good_send_good.setImageResource(resId);
    }

}